package com.linxz.wandnroid.presenter;
import com.linxz.framework.base.ActivityLifeCycleEvent;
import com.linxz.framework.base.presenter.BasePresenter;
import com.linxz.framework.protocol.callback.ModelCallBack;
import com.linxz.ui.pojo.BannerBean;
import com.linxz.wandnroid.appui.view.SystemView;
import com.linxz.wandnroid.module.WanAndroidModule;
import com.linxz.wandnroid.pojo.WandroidBannerBean;
import com.linxz.wandnroid.pojo.resp.SystemPageData;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.subjects.PublishSubject;

/**
 * @author Linxz
 * 创建日期：2020年02月27日 15:26
 * version：v4.5.4
 * 描述：
 */
public class SystemPresenter extends BasePresenter<SystemView, WanAndroidModule> {

    public SystemPresenter(SystemView mView, PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        super(mView, lifecycleSubject);
    }

    @Override
    public WanAndroidModule createModel(PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        return new WanAndroidModule(lifecycleSubject);
    }

    public void loadData(){
       mModel.getBannerAndNavigations(new ModelCallBack<SystemPageData>() {
           @Override
           public void onSuccess(SystemPageData model) {
               diffNavi(model);
           }

           @Override
           public void onErrors(String code, String msg) {
               mView.onLoadDataError(msg);
           }
       });
    }

    private void diffNavi(SystemPageData model){
        if (model.getNavigationBeans().size()==0){
            mView.onDataEmpty();
        }else{
            List<BannerBean> bannerBeans=new ArrayList<>();
            for (WandroidBannerBean bean:model.getBanners()){
                BannerBean bannerBean=new BannerBean();
                bannerBean.setId(0);
                bannerBean.setLinkCategory("c10001");
                bannerBean.setLinkUrl(bean.getUrl());
                bannerBean.setImgUrl(bean.getImagePath());
                bannerBeans.add(bannerBean);
            }
            mView.onLoadDataSuccess(bannerBeans,model.getNavigationBeans());
        }
    }
}
